package com.shreecon.lms.manager;
import static com.shreecon.lms.common.DateUtil.format;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.shreecon.lms.common.EmployeeDetailsView;
import com.shreecon.lms.common.LeaveDetailView;
import com.shreecon.lms.interfaces.LMSException;
import com.shreecon.lms.interfaces.manager.IEmployeeManager;
import com.shreecon.lms.interfaces.persistance.EmployeeDAOFactory;
import com.shreecon.lms.interfaces.persistance.IEmployeeDAO;
import com.shreecon.lms.interfaces.persistance.IShreeConsDAO;
import com.shreecon.lms.interfaces.persistance.ShreeConsDAOFactory;
import com.shreecon.lms.persistancy.hbm.EmployeeDetail;
import com.shreecon.lms.persistancy.hbm.LeaveAvailablity;

/**
 * The Class EmployeeManager.
 * 
 * @author Santhosh. Modified on Aug 13, 2011
 */
public class EmployeeManager implements IEmployeeManager{

    	private static EmployeeManagerValidator validator = new EmployeeManagerValidator();
	/* (non-Javadoc)
	 * @see com.shreecon.lms.interfaces.IEmployeeManager#getLoginDetail(java.lang.String, java.lang.String)
	 */
	public EmployeeDetailsView getLoginDetail(final String userName, final String password) throws LMSException{
	    validator.validateLoginDetail(userName, password);
	    IEmployeeDAO employeeDAO = EmployeeDAOFactory.getInstance().getEmployeeDAO();
	    final EmployeeDetail empDetail = employeeDAO.findByUserName(userName);
	    System.out.println(empDetail);
	    return convertToView(empDetail);
	}
	
	public EmployeeDetailsView getEmployeeById(Long empId) throws LMSException {
	    IShreeConsDAO shreeConsDAO = ShreeConsDAOFactory.getInstance().getShreeConsDAO();
	    final EmployeeDetail empDetail = (EmployeeDetail) shreeConsDAO.findById(empId, EmployeeDetail.class);
	    return convertToView(empDetail);
	}
	
	/**
	 * Convert to view.
	 * 
	 * @param empDetail
	 *            the emp detail
	 * @return the employee details view
	 */
	private EmployeeDetailsView convertToView(final EmployeeDetail empDetail) {
	    EmployeeDetailsView empView = new EmployeeDetailsView();
	    empView.setId(String.valueOf(empDetail.getEmpId()));
	    empView.setName(empDetail.getEmpName());
	    empView.setAddress(empDetail.getEmpAddress());
	    empView.setPhone(String.valueOf(empDetail.getEmpContactNumber()));
	    empView.setUserName(empDetail.getEmpUserName());
	    empView.setPassword(empDetail.getEmpPassword());
	    empView.setEmpRole(empDetail.getEmpRole());
	    empView.setDob(format(empDetail.getEmpDob()));
	    empView.setDoj(format(empDetail.getEmpDoj()));
	    empView.setDor(empDetail.getEmpDor()!=null?format(empDetail.getEmpDor()):null);
	    empView.setLeaveDetails(getLeaveViewList(empDetail.getLeaveAvailablities()));
	    return empView;
	}
	
	private List<LeaveDetailView> getLeaveViewList (Set<LeaveAvailablity> leaveSet) {
	    final List<LeaveDetailView> leaveViewList = new ArrayList<LeaveDetailView>( );
	    if( leaveSet != null ){
    	    	final List<LeaveAvailablity> leaveList = new ArrayList<LeaveAvailablity>( leaveSet);
    	    	for (LeaveAvailablity leaveAvailablity : leaveList) {
    	    	    final LeaveDetailView view = new LeaveDetailView();
    	    	    view.setLeaveId(String.valueOf(leaveAvailablity.getLaId()));
    	    	    view.setEmpDetail(leaveAvailablity.getEmployeeDetail());
    	    	    view.setLeaveName(leaveAvailablity.getLaName());
    	    	    view.setNumberOfDaysLeave(String.valueOf(leaveAvailablity.getLaNumDay()));
    	    	    leaveViewList.add(view);
    	    	}
	    }
	    return leaveViewList;
	}
	/*public static void main(String[] args) throws Exception{
		ShreeConsDAO consDAO = new ShreeConsDAO();
		//Emp detail Insert
		EmployeeDetail detail = new EmployeeDetail();
		detail.setEmpName("Kishore");
		detail.setEmpUserName("kishore");
		detail.setEmpPassword("shreecons");
		detail.setEmpAddress("Guindy");
		detail.setEmpContactNumber(9841066265L);
		detail.setEmpDob(new Date());
		detail.setEmpDoj(new Date());
//		consDAO.saveOrUpdate(detail);
		//Leave Availablity insert
//		LeaveAvailablity leaveDetail = (LeaveAvailablity)consDAO.findById(2L, LeaveAvailablity.class);
//		System.out.println(leaveDetail );
		EmployeeDetail employeeDetail = (EmployeeDetail)consDAO.findById(5L, EmployeeDetail.class);
		System.out.println(employeeDetail);
		LeaveAvailablity leaveAvailablity = new LeaveAvailablity();
		leaveAvailablity.setEmployeeDetail(employeeDetail);
		leaveAvailablity.setLaName("Causual Leave");
		leaveAvailablity.setLaNumDay(6L);
//		consDAO.save(leaveAvailablity);
		
		leaveAvailablity = new LeaveAvailablity();
		leaveAvailablity.setEmployeeDetail(employeeDetail);
		leaveAvailablity.setLaName("Sick Leave");
		leaveAvailablity.setLaNumDay(6L);
//		consDAO.save(leaveAvailablity);
		
	}*/
}
